package com.example.learn.hwod;

import java.util.*;

public class _37AI处理器组合 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] array = Arrays.stream(sc.nextLine().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll(" ","" ).split(",")).filter(s -> !s.isEmpty()).mapToInt(Integer::parseInt).toArray();
        int num = sc.nextInt();

        List<Integer> link1 = new ArrayList<>();
        List<Integer> link2 = new ArrayList<>();
        for (int index : array) {
            if (index>3) {
                link2.add(index);
            }else{
                link1.add(index);
            }
        }

        HashMap<Integer, int[]> map = new HashMap<>();
        map.put(1,new int[]{1,3,2,4});
        map.put(2,new int[]{2,4,3});
        map.put(4,new int[]{4});

        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if (num==8) {
            link1.addAll(link2);
            System.out.println(link1);
            return;
        }else{
            int[] ints = map.get(num);
            for (int core : ints) {
                if (link1.size()==core||link2.size()==core) {
                    if (link1.size()==core){
                        dfs(res,0,num,link1,new ArrayList<Integer>());
                    }
                    if (link2.size()==core){
                        dfs(res,0,num,link2,new ArrayList<Integer>());

                    }
                    break;
                }
            }
        }
        System.out.println(res);

    }

    private static void dfs(ArrayList<ArrayList<Integer>> res, int index, int level, List<Integer> list, ArrayList<Integer> integers) {
        if (integers.size()==level){
            res.add(new ArrayList<>(integers));
            return;
        }

        for (int i = index; i < list.size(); i++) {
            integers.add(list.get(i));
            dfs(res,index+1,level,list,integers);
            integers.remove(integers.size()-1);
        }
    }


}
